//
//  FreebaseSuggestSearch.h
//  Freebase
//
//  Created by Alec Flett on 12/12/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

#import <CoreLocation/CoreLocation.h>
#import "FreebaseRequest.h"
#import "FreebaseSuggest.h"

@class FreebaseSuggest;

@interface FreebaseSuggestSearch : UITableViewController
<CLLocationManagerDelegate, UISearchDisplayDelegate, UISearchBarDelegate>

{
    FreebaseSuggest* _suggestDelegate;
    NSArray *_currentResult;
    id<FreebaseRequest> _currentRequest;
    
    NSDictionary *_currentItem;

    BOOL _allowGeolocation;
    
    // should we show a "Create New" option?
    BOOL _allowCreateNew;

    // activity
    UIView *_activityView;
    UIActivityIndicatorView *_activityIndicator;
    UILabel *_activityLabel;
    
    // when this timer fires, it kicks off a search
    NSTimer *_searchTimer;
    
    
    // annoying, temporary - need a 'global' version of this
    CLLocationManager* _locationManager;
    CLLocation* _currentLocation;
    // have we yet registered for location updates?
    BOOL _awaitingLocation;
    
    // do we have a search ready to fire as soon as we know the location?
    BOOL _awaitingLocationForSearch;
    
    NSString* _searchString;
    NSInteger _searchScopeIndex;
    
    NSString* _preferredType;
    
    BOOL _strictType;

    // parameters for search/geosearch
    NSString* _mqlFilter;
    NSString* _mqlOutput;
    
    UITableView* _searchResultsTableView;
    
}

// set a timer to start a search
- (void)maybeBeginSearch:(NSString*)text scopeIndex:(NSInteger)scopeIndex;

// actually do the search
- (void)beginSearch;

// if the search is location-based, beginSearch calls this
-(void)beginGeosearch;

-(void)updateSearchOptions:(NSMutableDictionary*)options;

@property(nonatomic, assign) IBOutlet FreebaseSuggest* suggestDelegate;

// allow a "Create New..." button
@property(nonatomic, assign) BOOL allowCreateNew;
// allow geolocation
@property(nonatomic, assign) BOOL allowGeolocation;


// the current result list (private?)
@property(nonatomic, retain) NSArray* currentResult;

// the currently selected topic
@property(nonatomic, retain) NSDictionary* currentItem;

// the activityView in process - used to show/hide it
@property(nonatomic, retain) IBOutlet UIView* activityView;

// the label inside activityView
@property(nonatomic, retain) IBOutlet UILabel* activityLabel;

// the in-flight request (note only one at a time...)
@property(nonatomic, retain) id<FreebaseRequest> currentRequest;

@property(nonatomic, retain) NSString* searchString;
@property(nonatomic) NSInteger searchScopeIndex;

// the current location as per the location manager
@property(nonatomic, retain) CLLocation* currentLocation;
// defaults to "/common/topic"
@property(nonatomic, retain) NSString* preferredType;
// defaults to "False"
@property(nonatomic, assign) BOOL strictType;

// a MQL-filter for search or geosearch
@property(nonatomic, retain) NSString* mqlFilter;

@property(nonatomic, retain) NSString* mqlOutput;

@end
